﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS.Templates;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmLogin.xaml
    /// </summary>
    public partial class frmLogin : FormTrans
    {
        public bool Success = false;
        public string username = string.Empty;

        public frmLogin()
        {
            InitializeComponent();
            if (ViewModels.LoginViewModel.CheckRememberLogin())
                username = ViewModels.LoginViewModel.username;
            txtDataName.Text = ViewModels.LoginViewModel.DatabaseName;
            
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            //userCallForm.DynamicInvoke(sender);
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            //string M_LAN = SysLib.LanguageLib.GetLanguage();
            if (txtUsername.Text.Trim() == "")
            {
                MessageBox.Show(M_LAN == "V" ? "Chưa nhập tên đăng nhập" : "Please input username!", M_LAN == "V" ? "Lỗi" : "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtUsername.Focus();
                return;
            }
            if (txtPassword.Password.Trim() == "")
            {
                MessageBox.Show(M_LAN == "V" ? "Chưa nhập mật khẩu" : "Please input password!", M_LAN == "V" ? "Lỗi" : "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtPassword.Focus();
                return;
            }
            Success = ViewModels.LoginViewModel.Login(txtUsername.Text, txtPassword.Password);
            if (!Success)
            {
                MessageBox.Show(M_LAN == "V" ? "Đăng nhập thất bại" : "Login failed!", M_LAN == "V" ? "Lỗi" : "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtPassword.Focus();
                return;
            }
            else
            {
                username = txtUsername.Text.Trim();
                ViewModels.LoginViewModel.RememberLogin((bool)cbNhoMatKhau.IsChecked);
                ViewModels.LoginViewModel.SaveLogin();
                this.Close();
            }
        }

        private void txtUsername_GotFocus(object sender, RoutedEventArgs e)
        {
            // txtUsername.CaretIndex = txtUsername.Text.Length;
            txtUsername.SelectAll();
        }

        private void FrmLogin_Loaded(object sender, RoutedEventArgs e)
        {
            if (username != "")
            {
                ViewModels.LoginViewModel.SaveLogin();

                this.Close();
            }
            string OldUser = ViewModels.LoginViewModel.GetOldUsername();
            if (!string.IsNullOrEmpty(OldUser))
            {
                txtUsername.Text = OldUser;
                txtPassword.Focus();
            }
            else
                txtUsername.Focus();
        }

        private void FrmLogin_Closed(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
